package org.mspring.platform.counter;

import java.util.Objects;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.mspring.platform.counter.entity.Count;

/**
 *
 * @since 2014年4月25日
 * @author Gao Youbo
 */
public class CountKey {

    private String key1;
    private String key2;
    private String key3;
    private String key4;
    private String key5;
    private String countKey;

    private CountKey() {

    }

    public static CountKey getInstance(String... keys) {
        CountKey key = new CountKey();
        key.setKey1(getKey(0, keys));
        key.setKey2(getKey(1, keys));
        key.setKey3(getKey(2, keys));
        key.setKey4(getKey(3, keys));
        key.setKey5(getKey(4, keys));
        key.setCountKey(DigestUtils.md5Hex(key.getKey1() + "#" + key.getKey2() + "#" + key.getKey3() + "#" + key.getKey4() + "#" + key.getKey5()));
        return key;
    }

    public static CountKey getInstance(Count count) {
        CountKey key = new CountKey();
        key.setKey1(count.getKey1());
        key.setKey2(count.getKey2());
        key.setKey3(count.getKey3());
        key.setKey4(count.getKey4());
        key.setKey5(count.getKey5());
        key.setCountKey(count.getCountKey());
        return key;
    }

    private static String getKey(int index, String... keys) {
        if (keys == null || keys.length <= index) {
            return "";
        }
        return StringUtils.trimToEmpty(keys[index]);
    }

    public String getKey1() {
        return key1;
    }

    public void setKey1(String key1) {
        this.key1 = key1;
    }

    public String getKey2() {
        return key2;
    }

    public void setKey2(String key2) {
        this.key2 = key2;
    }

    public String getKey3() {
        return key3;
    }

    public void setKey3(String key3) {
        this.key3 = key3;
    }

    public String getKey4() {
        return key4;
    }

    public void setKey4(String key4) {
        this.key4 = key4;
    }

    public String getKey5() {
        return key5;
    }

    public void setKey5(String key5) {
        this.key5 = key5;
    }

    public String getCountKey() {
        return countKey;
    }

    public void setCountKey(String countKey) {
        this.countKey = countKey;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + Objects.hashCode(this.countKey);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CountKey other = (CountKey) obj;
        if (!Objects.equals(this.countKey, other.countKey)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return this.countKey;
    }

}
